var jstree = $.tree_create();
$(function() {
	jstree.init($('#tree'), {
		data: {
			type: 'json',
			async: true,
			url: 'tree-node/'
		},
		cookies: {},
		ui: {
			dots: false,
			animation: 250,
			context: false
		},
		rules: {
			deletable: ['folder'],
			dragrules: [
				'folder inside root',
				'folder inside folder',
				'folder before folder'
			]
		},
		lang: {
			new_node: 'Untitled folder',
			loading: ' '
		},
		callback: {
			onrgtclk: function(node, tree_obj, e) {
				e.preventDefault();
			},
			onmove: function(node, ref_node, type, tree_obj) {
				$.post('actions/move/', { id: $(node).attr('id'), ref_id: $(ref_node).attr('id'), type: type }, function(responseJson) {
				}, 'json');
			},
			beforechange: function(node, tree_obj) {
				//$('.richtext').each(function() { tinyMCE.execCommand('mceRemoveControl', false, $(this).attr('id')); });
			}, 
			onchange: function(node, tree_obj) {
				if (node.id) {
					$.get('?paging[page]=1&id='+node.id, function() {
						if ($(node).attr('rel')=='root') {
							$('a.delete').addClass('disabled');
						} else {
							$('a.delete').removeClass('disabled');
						}
						$('a.save').addClass('disabled');
						//initTinyMce();
					});
				}
			},
			ondelete: function(node, tree_obj) {
				$.post('actions/delete-folder/', { id: $(node).attr('id') }, function(responseJson) {
				}, 'json');
			},
			onload: function(tree_obj) {
				$('#tree').find('.no-children').removeClass('open').removeClass('closed').removeClass('no-children');
				if (!$('#tree a.clicked').length) {
					$('#tree a:first').click();
				}
			},
			onopen: function(node, tree_obj) {
				$(node).find('.no-children').removeClass('open').removeClass('closed').removeClass('no-children');
			}
		}
	});
	
	function jstree_create() {
		$.post('actions/create/', { parent_id: jstree.selected.attr('id') }, function(responseJson) {
			var already_opened = jstree.selected.find('li').length;
			jstree.open_branch(jstree.selected, false, function() {
				if (already_opened) {
					// children loaded
					var new_li = jstree.create({
						"attributes": {
							"id": responseJson.id,
							"rel": "folder",
							"parent_id": jstree.selected.attr('id')
						},
						"state": "closed",
						"data": {
							"title":"Untitled folder",
							"icon":"/admin/backend/resources/images/icons/folder.png"
						}
					});
					jstree.select_branch(new_li);
					new_li.removeClass('open').removeClass('closed');
				} else {
					// never been opened
					jstree.select_branch(jstree.get_node(jstree.selected.find('li:last-child')));
				}
				//
			});
			$("ul.tabs").tabs('select', 1);
		}, 'json');
	}

	
	$('.sidebar .toolbar a.create:not(.disabled)').livequery('click', function(e) {
		e.preventDefault();
		jstree_create();
	});
	$('.sidebar .toolbar a.delete:not(.disabled)').livequery('click', function(e) {
		e.preventDefault();
		jstree.remove();
	});
	
	
	$("ul.tabs").tabs();
	// TODO: ui should be the second parameter, but it's the third due to a but with Tabs - when it's fixed, this'll need changed.
	$("ul.tabs").bind('tabsselect', function(e, u, ui) {
		if ($(ui.tab).attr('href') == '#folder') {
			$('.content:not(.sidebar) .buttons').show();
		} else {
			$('.content:not(.sidebar) .buttons').hide();
		}
	});
	
	if (!$('#tree a.clicked').length) {
		$('#tree a:first').click();
	}


});

var swfu_settings = {
	upload_url: '/admin/global/files/actions/upload/',
	file_types: "*.*",
 	file_types_description: 'Files',
	file_size_limit: '100 MB'
};
